# _*_ coding: utf8
'''
author: ning.luo
create_date: 2019/03/07
last_update: 2019/03/07
'''

import unittest
import requests
import random
import json

class Punish_N_For_Fake(unittest.TestCase):
    '''假一罚N相关配置'''

    def setUp(self):
        self.headers = {"Content-Type": "application/json", "charset": "UTF-8"}
        self.base_url = "https://test-api.casstime.com/salepolicyv2-service"

    def add_or_update_configuration(self, punish_type, stand_name_list, quality_list):
        '''新增或者修改假一罚N政策设置'''
        url = self.base_url + "/casspunish/create"
        params = {
            "punishType": punish_type,
            "qualityStandNameConfigDTOS": [
                {
                    "cassAssureStandNames": stand_name_list,
                    "qualityDTOList": quality_list
                }
            ],
            "userLoginId": "ksmgg"
        }
        response = requests.post(url, data=json.dumps(params), headers=self.headers)
        try:
            self.assertEqual(200, response.status_code)
            print("新增成功！")
        except Exception as e:
            print("新增Http状态码：", response.status_code)
            print("新增响应结果：", response.text)
            print("新增失败原因：", e)

    def inquire_configuration(self, quality, quality_type, stand_name):
        '''查询假一罚N配置'''
        url = self.base_url + "/list/casspunish/query"
        params = {
            "quality": quality,
            "qualityTime": quality_type,
            "standName": stand_name
        }
        response = requests.post(url, data=json.dumps(params), headers=self.headers)
        j = json.loads(response.text)
        try:
            self.assertEqual(200, response.status_code)
            print(j)
            self.assertEqual(quality_type, j[0]["punishType"])
            self.assertEqual(quality, j[0]["qualityStandNameConfigDTOS"][0]["qualityDTOList"][0]["qualityType"])
            self.assertEqual(stand_name, j[0]["qualityStandNameConfigDTOS"][0]["cassAssureStandNames"][0]["standName"])
            print("查询成功！", quality_type+" "+stand_name+" "+quality)
        except Exception as e:
            print("查询Http状态码：", response.status_code)
            print("查询响应结果：", j)
            print("查询失败愿意：", e)

    def delete_configuration(self, punishType):
        '''删除假一罚N配置'''
        url = self.base_url + "/casspunish/delete/" + punishType
        response = requests.delete(url)
        self.assertEqual(200, response.status_code)
        print("删除成功！")

    def test(self):
        '''主流程测试'''
        for i in range(3):
            quality_list = [{"qualityType": "ORIGINAL_BRAND"}, {"qualityType": "EXTERNAL_BRAND"},
                            {"qualityType": "INTERNAL_BRAND"},
                            {"qualityType": "OTHER_BRAND"}, {"qualityType": "SECOND_HAND"},
                            {"qualityType": "ORIGINAL_OTHERS"},
                            {"qualityType": "ORIGINAL_CURRENCY"}]
            quality_num = random.randint(1, 7)
            quality_list1 = random.sample(quality_list, quality_num)

            punish_type_list= ["COMPENSATION_3TIME_POLICY", "COMPENSATION_10TIME_POLICY", "COMPENSATION_NOT_AVAILABLE"]
            punish_type = random.choice(punish_type_list)

            stand_name_list = [{"standName": "火花塞"}, {"standName": "叶子板"}, {"standName": "气门芯"}, {"standName": "节气门"},
                               {"standName": "PVC阀盖"}, {"standName": "进气管支架"}, {"standName": "空气流量计胶圈"},
                               {"standName": "空气管"},
                               {"standName": "大灯喷水盖"}, {"standName": "大灯喷水嘴"}, {"standName": "方向盘固定支架"},
                               {"standName": "副邦肽"},
                               {"standName": "油箱盖座"}, {"standName": "油箱隔热罩"}, {"standName": "冷凝器"},
                               {"standName": "冷凝器支架"},
                               {"standName": "车壳"}, {"standName": "挡泥板支架"}]
            stand_name_num = random.randint(1, 18)
            stand_name = random.sample(stand_name_list, stand_name_num)

            self.add_or_update_configuration(punish_type, stand_name, quality_list1)
            for j in range(stand_name_num):
                for k in range(quality_num):
                    self.inquire_configuration(quality_list1[k]["qualityType"], punish_type,
                                                           stand_name[j]["standName"])
            self.delete_configuration(punish_type)
            print("-------------------请求%d------------------"%(i+1))

    def tearDown(self):
        self.headers = None
        self.base_url = None

if __name__ == "__main__":
    unittest.main()